package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.AdminMapper;
import com.example.admin.pojo.Admin;
import com.example.admin.pojo.Permission;
import com.example.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin findAdminByUsernameAndPassword(String username,String password)
    {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, username);
        if(!StringUtils.isEmpty(password))
        {
            queryWrapper.eq(Admin::getPassword,password);
        }
        queryWrapper.last("limit 1");
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Permission> findPermissionByAdminId(Long adminId)
    {
        //select * from ms_permission where id in(select permission_id from ms_admin_permission where admin_id = 1)
        return adminMapper.findPermissionByAdminId(adminId);
    }
}
